package one.stand.service.impl;

import com.jmp.base.LabelApi;
import one.stand.advice.ResultEnum;
import one.stand.mapper.CompanyCustomerLableMapperExt;
import one.stand.model.CompanyCustomerLable;
import one.stand.model.ResultModel;
import one.stand.request.CompanyCustomerLableRequest;
import one.stand.response.CompanyCustomerLableResponse;
import one.stand.service.CompanyCustomerLableService;
import one.stand.util.AssertUtil;
import one.stand.util.RequestContext;
import one.stand.util.RequestContextHolder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author Administrator
 */
@Service
public class CompanyCustomerLableServiceImpl implements CompanyCustomerLableService {
    @Autowired
    private CompanyCustomerLableMapperExt companyCustomerLableMapperExt;

    @Override
    @Transactional
    public Integer addLable(CompanyCustomerLable lable) {
        AssertUtil.isTrue(StringUtils.isNoneBlank(lable.getLableName()), ResultEnum.PARAM_CHECK, "标签名称不能为空~！");
        AssertUtil.isTrue(lable.getLableName().length() <= 10, ResultEnum.PARAM_CHECK, "标签名称不能超过10个字~！");
        if (Objects.isNull(lable.getParentId()) || lable.getParentId() <= 0) {
            lable.setParentId(ROOT_PARENT_ID);
            AssertUtil.isNull(lable.getLableColor(), ResultEnum.PARAM_CHECK);
        }
        if (Objects.isNull(lable.getCompanyId())) {
            lable.setCompanyId(SYS_COMPANY_ID);
        }

        companyCustomerLableMapperExt.insertSelective(lable);
        return lable.getLableId();
    }

    @Override
    @Transactional
    public void delLable(Integer lableId) {
        AssertUtil.nonNull(lableId, ResultEnum.PARAM_CHECK);

        Integer count = companyCustomerLableMapperExt.selectTypeIdCount(lableId, LabelApi.LABEL_TYPE_COMPANY);
        AssertUtil.isTrue(count <= 0, ResultEnum.FAIL, "已在使用中，不能删除");

        CompanyCustomerLable inModel = companyCustomerLableMapperExt.selectByPrimaryKey(lableId);
        AssertUtil.nonNull(inModel, ResultEnum.NO_DATA);

        Integer parentId = inModel.getParentId() == null || inModel.getParentId() == 0 ? inModel.getLableId() : null;//如果为父标签，则删除其下所有子标签
        companyCustomerLableMapperExt.deleteByPrimaryKey(lableId, parentId);
    }

    @Override
    @Transactional
    public void updLable(Integer lableId, String lableName, String lableColor, Integer parentId) {
        AssertUtil.nonNull(lableId, ResultEnum.PARAM_CHECK);
        AssertUtil.isTrue(StringUtils.isNoneBlank(lableName), ResultEnum.PARAM_CHECK, "标签名称不能为空~！");
        AssertUtil.isTrue(lableName.length() <= 10, ResultEnum.PARAM_CHECK, "标签名称不能超过10个字~！");
        CompanyCustomerLable companyCustomerLable = new CompanyCustomerLable();
        companyCustomerLable.setLableId(lableId);
        companyCustomerLable.setLableName(lableName);
        companyCustomerLable.setLableColor(lableColor);
        companyCustomerLable.setParentId(parentId);
        companyCustomerLableMapperExt.updateByPrimaryKeySelective(companyCustomerLable);
    }

    @Override
    public ResultModel<List<CompanyCustomerLableResponse>> getTree(CompanyCustomerLableRequest request) {
        Integer parentId = Optional.ofNullable(request.getParentId()).orElse(0);
        final RequestContext.User user = RequestContextHolder.getRequestContext().getUser();
        Integer companyId = Objects.nonNull(user) ? user.getCompanyId() : null;
        List<CompanyCustomerLableResponse> models = companyCustomerLableMapperExt.getTree(parentId, companyId);
        return ResultModel.success(models);
    }

    @Override
    public List<CompanyCustomerLable> getLabels(int type, Integer typeId) {
        return companyCustomerLableMapperExt.getLabels(type, typeId);
    }
}
